package com.liuwei.classy.service.bizimpl;

import com.liuwei.classy.constant.CommonConst;
import com.liuwei.classy.dao.UserMapper;
import com.liuwei.classy.model.User;
import com.liuwei.classy.service.bizinter.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Service;

/**1
 * Created by qingshengzheng on 17/11/18.
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    HashOperations<String, String, Object> hashOperations;


    @Override
    public String sendMobileCode(String mobile) {
        return null;
    }

    @Override
    @CachePut(value="user",key="'USER_OPENID:'+#user.openid")
    public User registerUser(User user) {
        userMapper.addUser(user);
        return user;
    }
    public User registerByOpenid(String openid, String type) {
        User user = new User();
        if (CommonConst.TYPE_QQ.equals(type)) {
            user.setQq(openid);

        } else if (CommonConst.TYPE_WEIBO.equals(type)) {
            user.setWeibo(openid);

        } else if (CommonConst.TYPE_WEIXIN.equals(type)) {
            user.setWeixin(openid);
        }
        user.setOpenid(openid);
        User lastUser = registerUser(user);
        return lastUser;

    }

    @Override
    public void update(User user) {

    }

    /**111根据第三方openid登陆获取用户信息
     * @param openid
     * @param type
     * @return
     */
    @Override
    public User userLogin(String openid, String type) {
        User user = userDetailByOpenidAndType(openid, type);
        if (user == null)
            user = registerByOpenid(openid, type);
       return user;
    }


    @Override
    public User userLoginByCode(String mobile, String code) {
        return null;
    }

    @Override
    public Boolean isUserExist(String mobile) {
        return null;
    }

    @Override
    public void resetPassWord(String mobile, String deftPassWord) {

    }

    @Override
    public User userDetailByMobile(String mobile) {
        return null;
    }

    @Override
    public User userDetailByUid(Long uid) {
        User user= userMapper.queryUserByUid(uid);
        return user;
    }

    @Override
    public User userDetailByOpenidAndType(String openid, String type) {
        return userMapper.userDetailByOpenidAndType(openid, type);
    }
}
